package org.wutopia.labcloud.domain.uaa.controller;

import com.nimbusds.jose.JOSEException;
import lombok.AllArgsConstructor;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.AccountLoginInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.AccountRegisterInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserAccountEmailCodeInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.input.UserAccountLoginByEmailInputVO;
import org.wutopia.labcloud.domain.uaa.controller.vo.output.AccountOutputVO;
import org.wutopia.labcloud.domain.uaa.service.UserAccountService;
import org.wutopia.labcloud.library.common.base.CodedMessage;
import org.wutopia.labcloud.library.common.base.CommonResponse;


@RestController
@AllArgsConstructor
@RequestMapping("/user/account")
public class UserAccountController {

    private UserAccountService userAccountService;

    @PostMapping("/login_by_oauth2_gitee")
    public ResponseEntity<CodedMessage<AccountOutputVO>> loginByOauth2Gitee() {




        return null;
    }


    @GetMapping("/callback_oauth2_gitee")
    public ResponseEntity<CodedMessage<AccountOutputVO>> callback(@RequestParam String code) throws Exception {

        AccountOutputVO callback = userAccountService.callback(code);

        return null;
    }


    @PostMapping("/login")
    public ResponseEntity<CodedMessage<AccountOutputVO>> login(@RequestBody AccountLoginInputVO vo) throws Exception {

        AccountOutputVO accountOutputVO = userAccountService.loginrAccount(vo);
        return CommonResponse.successWithData(accountOutputVO);
    }


    @PostMapping("/register")
    public ResponseEntity<CodedMessage<AccountOutputVO>> register(@RequestBody AccountRegisterInputVO vo) throws JOSEException {


        AccountOutputVO accountOutputVO = userAccountService.registerAccount(vo);
        return CommonResponse.successWithData(accountOutputVO);
    }

    @PostMapping("/login_by_email")
    public ResponseEntity<CodedMessage<AccountOutputVO>> loginByEmail(@RequestBody UserAccountLoginByEmailInputVO vo) throws Exception {

        if(vo == null || vo.getCode() == null || vo.getEmail() == null ) {
            throw new Exception("入参错误");
        }

        AccountOutputVO accountOutputVO = userAccountService.loginByEmail(vo);
        return CommonResponse.successWithData(accountOutputVO);
    }

    @PostMapping("/email_code")
    public ResponseEntity<CodedMessage<String>> getEmailCode(@RequestBody UserAccountEmailCodeInputVO vo) {

        if(vo == null || vo.getEmail() == null) {
            CommonResponse.failure("入参错误");
        }

        String emailCode = userAccountService.getEmailCode(vo);


        return CommonResponse.successWithData(emailCode);
    }
}
